package com.css.dzj.dzjap.modules.jgxwother.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.apunit.entity.ApUnitEntity;
import com.css.dzj.dzjap.modules.jgxwother.entity.JgJgxwOtherActionEntity;
import com.css.dzj.dzjap.modules.jgxwother.service.OtherService;
import com.google.gson.annotations.JsonAdapter;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/other")
public class OtherController {

    @Autowired
    private OtherService otherService;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    //查询其他行为信息表
    @GetMapping(value = "/getOther")
    public R getOther(@RequestParam("actionId") String actionId) {
        String s = "";
        try {
            s = JSON.toJSONString(actionId);
            sysAuditService.saveSuccessAudit("其他行为信息查询成功", Constant.OTHER, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", otherService.getOther(actionId));
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("其他行为信息查询失败", Constant.OTHER, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }


    // 添加其他行为信息表
    @PostMapping(value = "/addOther")
    public R addOther(@RequestBody JgJgxwOtherActionEntity other) {
        String s = "";
        try {
            String flag = otherService.addOther(other);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                s = JSON.toJSONString(other);
                sysAuditService.saveSuccessAudit("添加成功", Constant.OTHER, Constant.PT, Constant.ADD, s, request);
                return r;
            } else {
                return R.error("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.OTHER, Constant.YZ, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    // 修改其他行为信息表
    @PostMapping(value = "/updateOther")
    public R updateOther(@RequestBody JgJgxwOtherActionEntity other) {
         String s = "";
        try {
            JgJgxwOtherActionEntity aOther = otherService.getOther(other.getActionId());
            BeanUtil.copyBeanNotNullToBean(other, aOther);
            aOther.setUpdateTime(new Date());
            otherService.update(aOther);
            s = JSON.toJSONString(other);
            sysAuditService.saveSuccessAudit("修改成功", Constant.OTHER, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改失败", Constant.OTHER, Constant.YZ, Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    // 删除其他行为信息表
    @PostMapping(value = "/deleteOther")
    public R deleteOther(@RequestParam("actionId") String actionId) {
        String s = "";
        try{
            if (otherService.deleteOther(actionId)) {
                s = JSON.toJSONString(actionId);
                sysAuditService.saveSuccessAudit("删除成功", Constant.OTHER, Constant.PT, Constant.DEL, s, request);
                return R.ok("操作成功!");
            } else {
                return R.error("操作失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公示通报删除失败", Constant.OTHER, Constant.YZ, Constant.DEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询所有其他行为信息表
     */
    @GetMapping(value = "/getAllOther")
    public R getAllOther(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize
    ) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults results = otherService.getAllOther(pageRequest);
            s = JSON.toJSONString(results);
            sysAuditService.saveSuccessAudit("行为信息查询成功", Constant.OTHER, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("data", results);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("行为信息查询失败", Constant.OTHER, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询所有其他行为信息表
     */
    @GetMapping(value = "/getAllConOther")
    public R getAllConOther(@RequestParam String superviseItemCode,
                            @RequestParam(required = false) String recordUniqueIdentity,
                            @RequestParam(required = false) String actionName,
                            @RequestParam(required = false) String actionCode,
                            @RequestParam(required = false) String checkBehaviorNumber,
                            @RequestParam(required = false) String superviseObject,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize) {
        String s ="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults results = otherService.getAllConOther(pageRequest, superviseItemCode, recordUniqueIdentity, actionName, actionCode, checkBehaviorNumber, superviseObject);
            s = JSON.toJSONString(results);
            sysAuditService.saveSuccessAudit("行为信息查询成功", Constant.OTHER, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("data", results);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("行为信息查询失败", Constant.OTHER, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 获取其他行为
     * @return
     */
    @GetMapping("/other")
    public R countOther() {
        HashMap count = otherService.getCount();
        return R.ok().put("count", count);
    }

    /***
     * 获取其他行为
     * @return
     */
    @GetMapping("/otheryear")
    public R countOtherByYear(String year) {
        HashMap count = otherService.getCountByYear(year);
        return R.ok().put("count", count);
    }

    /***
     * 获取当前登陆人的机构的其他行为
     * @param request
     * @return
     */
    @GetMapping("/getother")
    public R getOpOther(HttpServletRequest request) {
        Long count = otherService.getOpCheck(request);
        return R.ok().put("count", count);
    }


    /******************************** 门户接口-开始 ********************************/

    @GetMapping(value = "/getPage")
    public QueryResults getPage(@RequestParam(required = false) String superviseItemCode,
                                @RequestParam(required = false) String recordUniqueIdentity,
                                @RequestParam(required = false) String actionName,
                                @RequestParam(required = false) String actionCode,
                                @RequestParam(required = false) String checkBehaviorNumber,
                                @RequestParam(required = false) String superviseObject,
                                @RequestParam(required = false) Integer pageNo,
                                @RequestParam(required = false) Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults<JgJgxwOtherActionEntity> page = otherService.getAllConOther(pageRequest,
                superviseItemCode, recordUniqueIdentity, actionName, actionCode, checkBehaviorNumber, superviseObject);
        return page;
    }

    /**
     * 获取其他行为List
     *
     * @return
     */
    @GetMapping(value = "/getList")
    public List getList(@RequestParam(required = false) String superviseItemCode,
                        @RequestParam(required = false) String recordUniqueIdentity,
                        @RequestParam(required = false) String actionName,
                        @RequestParam(required = false) String actionCode,
                        @RequestParam(required = false) String checkBehaviorNumber,
                        @RequestParam(required = false) String superviseObject) {
        List<JgJgxwOtherActionEntity> list = otherService.getList(
                superviseItemCode, recordUniqueIdentity, actionName, actionCode, checkBehaviorNumber, superviseObject);
        return list;
    }

    /**
     * 根据ids(逗号分隔)查询List数据
     *
     * @param ids
     * @return
     */
    @GetMapping("/getListByIds")
    public List exportToExcel(@RequestParam String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<JgJgxwOtherActionEntity> list = otherService.queryByIds(idList);
        return list;
    }

    /**
     * 获取总数
     *
     * @return
     */
    @GetMapping("/getTotalCount")
    public Integer getTotalCount() {
        Integer count = otherService.getTotalCount();
        return count;
    }
    /********************************* 门户接口-结束 *******************************/

    /**
     *
     * 查询行为信息其他行为所有信息
     * 对外提供接口
    *@Author weitao
    *@Date 17:11 2020/6/28
    */
    @GetMapping(value ="/getApList")
    public List<JgJgxwOtherActionEntity> getApList(){

        try {
            List<JgJgxwOtherActionEntity> list = otherService.getApList();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
